#include "Cylinder.h"
#include "Camera.h"

Cylinder::Cylinder(double radius, double height, const QString &name) :
    Item(name)
{
    // degrees between two points around the cylinder
    double spacing = 20.0;

    /* FIXME complete the code */
    double angle = M_PI*spacing/180;
    Face face(Face::TriangleStrip);
    face.setColor(QColor(50,120,255));
    double theta, rTheta;


    for( theta = 0; theta <= 360; theta+=spacing){
        rTheta = theta / 180.0 * M_PI;

//        Vector n0 = Matrix::rotationX(rTheta) * Matrix::translation(height/2.0,0.0,0.0) * Vector::ORTHO_Z;
//        Vector n1 = Matrix::rotationX(rTheta) * Matrix::translation(-height/2.0,0.0,0.0) * Vector::ORTHO_Z;


        Vector n0 = Matrix::translation(-height/2.0,0.0,0.0) * Matrix::rotationX(rTheta) * Vector::ORTHO_Z;
        Vector n1 = Matrix::translation(height/2.0,0.0,0.0) * Matrix::rotationX(rTheta) * Vector::ORTHO_Z;
        Vector nor = Matrix::translation(0.0,0.0,0.0) * Matrix::rotationX(rTheta) * Vector::ORTHO_Z;

        Vector v0 = n0  * radius;
        Vector v1 = n1  * radius;

        face.addPoint(v0, nor);
        face.addPoint(v1, nor);
qDebug()<<theta<<n0<<n1<<v0<<v1<<(n0 + n1);

    }

//    for(int i = 0; i <= 360/spacing; i++){
//        double cosAlpha = cos(angle*i);
//        double sinAlpha = sin(angle*i);
//        // height in axis x
//        Vector p( height/2, radius*sinAlpha,radius*cosAlpha);
//        Vector p2(-height/2, radius*sinAlpha, radius*cosAlpha);
//        // vector cylinder
//        Vector n(0, radius*sinAlpha, radius*cosAlpha);
//        // addpoint
//        face.addPoint(p, n);
//        face.addPoint(p2, n);
//    }

    addFace(face);
    /* STOP here */
}

void Cylinder::onFrameChanged(double frame)
{
    Q_UNUSED(frame);
    /* FIXME complete the code */
    // (optional)
    // apply a transformation matrix on this item using setGLTransform()    
    // and frame to make the cube move.

    /* STOP here */
}


